{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# 包（Packages）\n",
    "\n",
    "Julia有超过2000个登记在案的包，这使包成为Julia生态中很大的一个部分。\n",
    "\n",
    "尽管如此，包生态依然有成长的必要。值得一提的是，我们有最棒的调用其他语言的函数，提供了很棒的其他语言函数接口。我们可以很容易地通过`PyCall`或`Rcall`调用Python或R。\n",
    "\n",
    "这意味着你不必等到Julia的生态完全成熟，而且迁移到Julia并不意味着放弃你在其他语言中惯用的包或者库！\n",
    "\n",
    "查看全部可获得的包，请看\n",
    "\n",
    "https://pkg.julialang.org/\n",
    "或者\n",
    "https://juliaobserver.com/\n",
    "\n",
    "现在我们先学习一下该如何使用包。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在已有的Julia中第一次要使用一个包的时候，你需要用包管理器（package manager）明确添加（add）它："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "using Pkg\n",
    "Pkg.add(\"Example\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "每次使用Julia（比如打开一个新的REPL回话或者第一次打开一个notebook），你需要通过`using`关键字载入要用的包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "using Example"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在`Example.jl`的源码\n",
    "https://github.com/JuliaLang/Example.jl/blob/master/src/Example.jl\n",
    "中我们可以看到声明了下面这个函数\n",
    "\n",
    "```\n",
    "hello(who::String) = \"Hello, $who\"\n",
    "```\n",
    "\n",
    "载入了`Example`我们现在应该可以调用`hello`函数了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hello(\"it's me. I was wondering if after all these years you'd like to meet.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "现在让我们来戏耍Colors包吧"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Pkg.add(\"Colors\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "using Colors"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们来创建一个100个不同颜色的调色板"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "palette = distinguishable_colors(100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "然后我们通过`rand`函数来创建随机矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rand(palette, 3, 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在下一个notebook中，我们将使用一个新的包来绘制数据集。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 练习\n",
    "\n",
    "#### 7.1\n",
    "载入Primes包（源码在https://github.com/JuliaMath/Primes.jl ）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "using Primes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "deletable": false,
    "editable": false,
    "hide_input": true,
    "nbgrader": {
     "checksum": "0c808f5cb8f48b17f8552f00abf2ee09",
     "grade": true,
     "grade_id": "cell-7c4498760f1c26d7",
     "locked": true,
     "points": 1,
     "schema_version": 1,
     "solution": false
    }
   },
   "outputs": [],
   "source": [
    "@assert @isdefined Primes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 7.2\n",
    "确认你可以使用`primes`函数获得所有小于1,000,000的质数，并把它存入变量`primes_list`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "deletable": false,
    "editable": false,
    "hide_input": true,
    "nbgrader": {
     "checksum": "ec7f3d8aa43d629a2e57bc4be8525286",
     "grade": true,
     "grade_id": "cell-ab26afd81c0a11ae",
     "locked": true,
     "points": 1,
     "schema_version": 1,
     "solution": false
    }
   },
   "outputs": [],
   "source": [
    "@assert primes_list == primes(1000000)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "完成练习后请点击顶部的`Validate`。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 1.0.5",
   "language": "julia",
   "name": "julia-1.0"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.0.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
